#!/bin/sh
# Job yaml can send below ENVIRONMENT VARIABLES to me
# - events
# END ENVIRONMENT VARIABLES

. $LKP_SRC/lib/upload.sh

[ -n "$events" ] || exit 0

test_save_time_delta()
{
	[ "$HOSTNAME" = avoton2 ] &&
	grep -q -F uptime /sys/kernel/debug/tracing/trace_clock && {
		echo uptime > /sys/kernel/debug/tracing/trace_clock
		echo 0 > $TMP/time_delta
		return
	}

	echo 0 > /sys/kernel/debug/tracing/trace
	local date_ts="$(date '+%s.%N')"
	echo a long long timestamp detect string > /sys/kernel/debug/tracing/trace_marker
	local trace_ts="$(grep -m1 -o '[0-9.]\+: tracing_mark_write: a long long timestamp detect string' /sys/kernel/debug/tracing/trace)" || {
		echo "Failed to detect ftrace time delta."
		exit 0
	}
	echo "$date_ts - ${trace_ts%%:*}" | bc > $TMP/time_delta
	echo 0 > /sys/kernel/debug/tracing/trace
}
test_save_time_delta

mkdir $TMP/trace_event

for event in $events
do
	for knob in /sys/kernel/debug/tracing/events/*/$event/enable
	do
		[ -e "$knob" ] || break
		echo "echo 1 > $knob" >> $RESULT_ROOT/reproduce.sh
		echo 1 > $knob
	done

	cp /sys/kernel/debug/tracing/events/*/$event/format $TMP/trace_event/${event}.fmt
done

upload_files $TMP/trace_event

echo 1 > /sys/kernel/debug/tracing/tracing_on

exec awk '!x[$5 substr($4, 0, length($4)-4)]++' /sys/kernel/debug/tracing/trace_pipe
